/**
 * Created by shudong.msd(风骐) on 2016/4/16.
 */

var app = angular.module("webApp",['ui.router','ngTagsInput','ui.grid','ui.grid.pagination',
    'ui.grid.resizeColumns', 'ui.grid.moveColumns','ui.grid.autoResize','infinite-scroll','ngAnimate']);

/**
 * 由于整个应用都会和路由打交道，所以这里把$state和$stateParams这两个对象放到$rootScope上，方便其它地方引用和注入。
 * 这里的run方法只会在angular启动的时候运行一次。
 * @param  {[type]} $rootScope
 * @param  {[type]} $state
 * @param  {[type]} $stateParams
 * @return {[type]}
 */
app.run(function($rootScope, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;

    // 动画
    $rootScope.mzAnimate = 'toggle';

    //'slide-top','slide-down','bouncy-slide-down','bouncy-slide-top','bouncy-slide-left','bouncy-slide-right','bouncy-scale-in',
    var flys = ['toggle','spin-toggle','scale-fade','scale-fade-in',
        'flip-in','slide-left','slide-right', 'rotate-in'];

    $rootScope.updateAnimate = function(){
        $rootScope.mzAnimate = flys[parseInt(flys.length*Math.random())];
    };
    /*$rootScope.$on('$stateChangeSuccess', function(event, to, toParams, from, fromParams) {
        $rootScope.$previousState = from;
    });*/
});


app.config(function ($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/login');
    $urlRouterProvider.when('','/login');

    $urlRouterProvider.when(/.+_pane/, ['$state', function ($state) {
        $state.go('main.index.detail');
    }]);

    $urlRouterProvider.when(/((base-)|(star-)).+/, ['$state','$stateParams', function ($state,$stateParams) {
        if(sessionStorage.getItem('whereIam')=='result'){
            if(!$state.is('main.sResult.detail',{'type':sessionStorage.getItem('stateType')})){
                $state.go('main.sResult.detail',{'type':sessionStorage.getItem('stateType')});
            }
        }else if(sessionStorage.getItem('whereIam')=='search'){
            if(!$state.is('main.search.detail',{'type':sessionStorage.getItem('stateType')})){
                $state.go('main.search.detail',{'type':sessionStorage.getItem('stateType')});
            }
        }

    }]);

    /*$urlRouterProvider.when(/.+Mission$/, ['$state', function ($state) {
        $state.go('main.mission');
    }]);*/

    $stateProvider
        .state("login", {
                    url: "/login",
                    templateUrl: "tpls/login/login.html",
                    controller:'loginFormCtrl'
        }).state('main',{
            templateUrl:'tpls/layout/main.html',
            controller:'navCtrl'
        }).state('main.index',{
            templateUrl:'tpls/layout/index.html'
        }).state('main.index.detail',{
            url:'^/index',
            resolve:{
                'dataListService':'indexDataListService',
                'datepickerService':'datepickerService',
                'keywordsService':'indexKeywordsService',
                'searchService':'indexSearchService'
            },
            views:{
                /*'@main.index':{
                    templateUrl: function (stateParams){
                        if(stateParams.type==''){
                            return "tpls/index/tv_pane.html";
                        }
                        return "tpls/index/"+stateParams.type+"_pane.html";
                    },
                    controllerProvider: function($stateParams) {
                        if($stateParams.type=='star'){
                            return "indexStarSearchCtrl";
                        }else if($stateParams.type=='variety'){
                            return "indexVarietySearchCtrl";
                        }else{
                            return 'indexTVSearchCtrl';
                        }
                    }
                },*/
                'tv@main.index':{
                    templateUrl: "tpls/index/tv_pane.html",
                    controller:'indexTVSearchCtrl'
                },
                'variety@main.index':{
                    templateUrl: "tpls/index/variety_pane.html",
                    controller:'indexVarietySearchCtrl'
                },
                'star@main.index':{
                    templateUrl: "tpls/index/star_pane.html",
                    controller:'indexStarSearchCtrl'
                },
                'tvList@main.index':{
                    templateUrl: "tpls/index/tvList.html",
                    controller:'indexTVListCtrl'
                },
                'varietyList@main.index':{
                    templateUrl: "tpls/index/varietyList.html",
                    controller:'indexVarietyListCtrl'
                }
            }
        }).state('main.sResult',{
            templateUrl:'tpls/layout/result.html'
        }).state('main.sResult.detail',{
            url:'^/search_result/{type}',
            resolve:{
                'dataFetcher':'resultDataFetcher'
            },
            views:{
                'waiting':{
                    templateUrl: "tpls/common/waiting.html",
                    controller:'commonWaitingCtrl'
                },
                'filterBox':{
                    templateUrl:'tpls/result/filterbox.html',
                    controller:'resultFilterBoxCtrl'
                },
                'dataGrid':{
                    templateUrl:'tpls/result/datagrid.html',
                    controller:'dataGridCtrl'
                }
            }
        }).state('main.search',{
            templateUrl:'tpls/layout/search.html'
        }).state('main.search.detail',{
            url:'^/search/:type',
            resolve:{
                'dataFetcher':'searchDataFetcher'
            },
            views:{
                'filterbox@main.search':{
                    templateUrl:'tpls/search/search.html',
                    controller:'searchFilterBoxCtrl'
                }
            }
        }).state('main.mission',{
            templateUrl:'tpls/mission/mission_center.html',
            resolve:{
                'dataFetcher':'missionDataFetcher'
            },
            controller:'missionController'
        }).state('main.mission.all',{
            url:'^/missioncenter/all',
            templateUrl:'tpls/mission/all_mission.html',
            controller:'allMissionController'
        }).state('main.mission.unread',{
            url:'^/missioncenter/unread',
            templateUrl:'tpls/mission/unread_mission.html',
            controller:'unreadMissionController'
        }).state('main.user',{
            url:'^/usercenter',
            templateUrl:'tpls/user/user_center.html',
            controller:'userCenterController'
        });

});

